// pages/Invitation/Invitation.js
var total_micro_second =1;

/* 毫秒级倒计时 */
function countdown(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: dateformat(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    // timeout则跳出递归
    return;
  }
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 500;
    countdown(that);
  }
    , 500)
}

// 时间格式化输出，如3:25:19 86。每500ms都会调用一次
function dateformat(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600 % 24);
  // 分钟位
  var min = Math.floor((second - Math.floor(second / 3600) * 3600) / 60);
  // 秒位
  var sec = (second - Math.floor(second / 3600) * 3600 - min * 60);// equal to => var sec = second % 60;
 
  var date = Math.floor(second / 3600 / 24)
  var data = { date, hr, min, sec}
  return data;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clock: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/mobile/api/TeamBuy/GetTeamBuyInfo';
    var that = this
    wx.request({
      url: url,
      data: {
        custId: custId,
        unionStoreID: options.unionStoreID,
        teamId: options.teamId
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var json = JSON.parse(res.data.content); 
        total_micro_second = Date.parse(json.Team.EndTime) - Date.parse(json.Now);
        countdown(that);
        that.setData({
          TeamLeader: json.TeamLeader,
          Team: json.Team,
          UserList: json.UserList,
          Shop: json.Shop,
          Product: json.Product,
          DiscountedPrice: json.Product.CurrentPrice - json.Team.Discount,
          options: options
        })

      }
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var that = this
    console.log(that.options.custId)
    return {
      title: '一起来拼团吧',
      path: '/pages/Invitation/Invitation?custId=' + custId + "&unionStoreID=" + that.options.unionStoreID + "&teamId=" + that.options.teamId
    }
  }
})